package com.algorithm.fifty2onehundred;

/**
 * @author tuzhijin
 * @version Id: MaxSubArray_53.java, v 0.1 2021/5/22 上午8:21 tuzhijin Exp $$
 */
public class MaxSubArray_53 {

    public static void main(String[] args) {
        int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
        System.out.println(maxSubArray(nums));
    }

    public static int maxSubArray(int[] nums) {
        int pre = 0, result = nums[0];
        for (int num : nums) {
            pre = Math.max(pre + num, num);
            // 取f(i-1)+nums[i]和nums[i]的大值
            result = Math.max(pre, result);
        }
        return result;
    }

}
